import Vue from 'vue'
import VueRouter from 'vue-router'
import NewBooksVue from '../components/Home/NewBooks.vue'
import Home from '../views/Home.vue'
import NewBooks from '../components/Home/NewBooks.vue'
import ShoppingCart from '../views/ShoppingCart.vue'
import SettleAccounts from '../views/SettleAccounts.vue'

Vue.use(VueRouter);


const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
	// 首页
	{
		path: '/Home',
		name: 'Home',
		component: Home,
		meta:{
			title: '书城 - 寻找自己喜欢的一本书'
		}
	},
	{
		path: '/No:id',
		component: () => import('../components/notfound.vue'),
	},
	// 搜索页面
	{
		path: '/search',
		name: 'Search',
		component: () => import('../views/Search.vue'),
		meta:{
			title: '搜索结果'
		}
	},
	// 新书页面
	{
		path: '/newbooks',
		name: 'NewBooks',
		component: NewBooks,
		meta:{
			title: '新书上市'
		}
	},
	// 图书详情
	{
			path: '/book_detail:id',
			name: 'BookDetail',
			component: () => import('../views/BookDetail.vue'),
			props: true,
	},
	// 图书分类
	{
		path: '/book_sort:id',
		name: 'BookSort',
		component: () => import('../views/BookSortPage.vue'),
			meta:{
				title: '图书分类'
			}
		},
	// 结算
	{
		path: '/settleaccounts',
		name: 'SettleAccounts',
		component: SettleAccounts,
		meta:{
			title: '确认订单'
		}
	},
	{
		path: '*',
		redirect: '/Home'
	},
]

const router = new VueRouter({
  //mode: 'history',
  base: process.env.BASE_URL,
  routes
})


router.beforeEach((to, from, next) => {
	// 修改标题
	window.document.title = to.meta.title == undefined ? 'Page Not Found':to.meta.title;
	
	
	next()
	
})


export default router
